BookTypeForm = function(cfg){
	BookTypeForm.superclass.constructor.call(this, cfg);
};
top.Ext.extend(BookTypeForm, top.Ext.form.FormPanel, {
	initComponent: function(){
		top.Ext.apply(this, {
		    baseCls: 'x-plain',
		    labelWidth: 60,
		    labelAlign: "right",
		    defaultType: 'textfield',
		    items: [{
		    	xtype: 'hidden',
		        name: 'bookType.id'
		    },{
		        fieldLabel: '类别名称',
		        name: 'bookType.name',
		        allowBlank:false,
		        anchor:'99%'
		    },{
		    	xtype: 'numberfield',
		        fieldLabel: '排序号',
		        name: 'bookType.seq',
		        allowBlank:false,
		        anchor: '99%'
		    }]
		});
		BookTypeForm.superclass.initComponent.call(this);
	},
	save: function(){
		var self = this;
		var f = self.getForm();
    	if (!f.isValid()){
    		MyUtils.hint('表单验证不通过');
    		return;
    	}
    	MyUtils.waitSubmitting();
    	f.submit({
            url:'book-type!save.action',
			success: function(form, action) {
				var type = action.result.data;
				//如果是修改，重新加载类别树数据，如果是添加，则在树后面追加节点
				self.win.bookTypeId ? bookMgr.reloadBookType() : bookMgr.addBookTypeNode(type);
    	    	self.win.close();
			},
			failure: function(form, action) {
     	    	MyUtils.actionError(action, '保存失败');
			}
    	});
	},
	fillData: function(values){
		this.getForm().setValues(values);
	},
	firstFocus: function(){
		//this.getForm().items.itemAt(1).focus(true, 200);
		this.getForm().findField('bookType.name').focus(true, 300);
	}
});

BookTypeWin = function(cfg){
	BookTypeWin.superclass.constructor.call(this,cfg);
};
top.Ext.extend(BookTypeWin, top.Ext.Window, {
	initComponent: function(){
		top.Ext.apply(this, {
		    width: 250,
		    height: 130,
		    layout: 'fit',
		    plain:true,
		    modal: true,
		    bodyStyle:'padding:5px;',
		    iconCls: 'icon-window',
		    buttonAlign:'right',
		    items: new BookTypeForm({win: this}),
		    listeners: {
		    	show: function(){ //填充表单，设置焦点
		    		this.fillForm();
		    		this.firstFocus();
		    	}
		    },
		    buttons: [{
		        text: '保存',
		        handler: function(){this.submitForm();},
		        iconCls: 'icon-save',
		        scope: this
		    },{
		        text: '关闭',
		        handler: function(){this.close();},
		        iconCls: 'icon-close',
		        scope: this
		    }]
		});
		BookTypeWin.superclass.initComponent.call(this);
	},
	fillForm: function(){
		var self = this;
		var bookTypeId = this.bookTypeId || 0;
		var url = String.format('book-type!get.action?bookTypeId={0}', bookTypeId);
		MyUtils.getJson(url, true, function(bookType){
			self.getForm().fillData(MyUtils.toFormValues(bookType, 'bookType.'));
		});
	},
	submitForm: function(){
		this.getForm().save();
	},
	firstFocus: function(){
		this.getForm().firstFocus();
	},
	getForm: function(){
		return this.items.itemAt(0);
	}
});

function addBookType(){
	new BookTypeWin({title: '添加类别'}).show();
}

function modifyBookType(){
	var node = bookMgr.getCurBookType();
	if (!node || node.id === 'root'){
		MyUtils.showMsg('提示', '请选择要修改的类别！', 'w');
		return;
	}
	new BookTypeWin({title: '修改类别',bookTypeId: node.id}).show();
}

function removeBookType(){
	var node = bookMgr.getCurBookType();
	if (!node || node.id === 'root'){
		MyUtils.showMsg('提示', '请选择要删除的类别！', 'w');
		return;
	}
	top.Ext.Msg.confirm('提示','确定要删除该类别吗？', function(btn){
		if (btn == 'yes'){
			var url = String.format('book-type!remove.action?bookTypeId={0}', node.id);
			MyUtils.request(url, true, function(){
				bookMgr.removeTypeNode(node)
			}, function(response, options){
				//MyUtils.responseError(response, '删除失败');
			});
		}
	});
}
